/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package findthebomb;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import linkedlist.MyLinkedListS;

/**
 *
 * @author Islam Hamdi
 */
public class MapFile {
    /** extracting information about the game from the map file*/
    public static MyLinkedListS readMap() throws FileNotFoundException, IOException{
        FileReader file = new FileReader("MapFile.txt");
        BufferedReader buff = new BufferedReader(file);
	String levels = buff.readLine();
	int nLevels = Integer.parseInt(levels.substring(levels.indexOf("=")+1,
				levels.length()));

        // regrouping grids of different levels in a Linked List.
        MyLinkedListS<Grid> maps=new MyLinkedListS<Grid>();
	for(int index=0;index<nLevels;index++){
            String levelN=buff.readLine(); //level number
            int levelNumber=Integer.parseInt(levelN.substring(levelN.indexOf("=")+1,levelN.length()));

            String dimensionN =buff.readLine(); // dimensions of the grid.
            int dimensions=Integer.parseInt(dimensionN.substring(dimensionN.indexOf("=")+1,dimensionN.length()));
            Grid map=new Grid(dimensions);

            for(int counter=0;counter<dimensions;counter++){
                String row=buff.readLine();

                //each row has (nDimensions) column
                while(row.length()!=dimensions){
                    row+=" ";
                }
                // Storing each row of the map.
                for(int col=0;col<dimensions;col++){
                    map.setMap(counter,col,row.charAt(col));
                }
            }
            maps.add(levelNumber,map);
	}
          return maps;
    }
}
